package com.coder520.mamabike.SendSMS;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.coder520.mamabike.common.Parameter;
import com.coder520.mamabike.security.MD5Util;
import com.coder520.mamabike.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ASUS on 2017/8/15.
 */
@Component("sendMessage")
public class MiaoDISMS implements SendMessage {

    private static Logger logger = LoggerFactory.getLogger(MiaoDISMS.class);

    @Autowired
    private Parameter parameter;

    @Override
    public void send(String phone, String tepId, String params) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

        String timestaamp = format.format(new Date());

        String sig = MD5Util.getMD5(parameter.getACCOUNT_SID() + parameter.getAUTH_TOKEN()+timestaamp);

        String url = parameter.getREST_URL() + "/industrySMS/sendSMS";

        Map<String, String> param = new HashMap<>();

        param.put("accountSid", parameter.getACCOUNT_SID());

        param.put("to", phone);

        param.put("templateid", tepId);

        param.put("param", params);

        param.put("timestamp",timestaamp);

        param.put("sig", sig);

        String result = HttpUtil.post(url, param);

        JSONObject jsonObject =  JSON.parseObject(result);

        if(!jsonObject.getString("respCode").equals("00000")){
            logger.error("发送短信失败");
        }

    }
}
